package solucionessgh.Habitacion;


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 * @author roderick
 **/
public class HabitacionModel extends DataBase{
    
    private int id;//habid
    private int ehacod;
    private String habcod;
    private String habdesc;

    public HabitacionModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("insert into habitacion(ehacod,habdesc,habcod) values(?,?,?);");
                stmt.setInt(1,this.ehacod);
                stmt.setString(2,this.habdesc);
                stmt.setString(3,this.habcod);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("update habitacion set ehacod = ? ,habdesc = ?, hadcod = ? where habcod = ? ;");
                stmt.setInt(1,this.ehacod);
                stmt.setString(2,this.habdesc);
                stmt.setString(3,this.habcod);
                stmt.setInt(4, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from habitacion where habid=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT * FROM habitacion order by habdesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT h.habcod, h.habdesc, eh.ehadesc as ehacod,h.habid FROM habitacion h INNER JOIN estadohabitacion eh ON h.ehacod=eh.ehacod   WHERE h.habdesc LIKE '"+var+"%' order by habcod");
    }

    public ResultSet finder(int var){
        return this.find("SELECT habcod, habdesc, ehacod, habcod ,habid FROM habitacion WHERE habid = "+var+";");
    }

    

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getHabdesc() {
        return habdesc;
    }

    public void setHabdesc(String habdesc) {
        this.habdesc = habdesc;
    }

     public int getEhacod() {
        return ehacod;
    }

    public void setEhacod(int ehacod) {
        this.ehacod = ehacod;
    }


    public String getHabcod() {
        return habcod;
    }

    public void setHabcod(String habcod) {
        this.habcod = habcod;
    }
}
